package cn.cnf.controller;

import cn.cnf.common.Result;
import cn.cnf.common.enums.StatusCode;
import cn.cnf.common.exception.CustomException;
import cn.cnf.entity.Chapter;
import cn.cnf.service.ChapterService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 服务控制器
 *
 * @author ZhuMeiYi
 * @description
 * @since 2020-12-04 15:18:43
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/chapter")
public class ChapterController {
    @Autowired
    private ChapterService chapterService;


    /**
     * 新增章节
     *
     * @param chapter
     * @return
     */
    @PostMapping("/addOrUpdateChapter")
    public Result addOrUpdateChapter(@RequestBody Chapter chapter) {
        try {
            return Result.success(chapterService.addOrUpdateChapter(chapter));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }
}